package guda.house.web.action.bill;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.BillPayBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.enums.SysVarTypeEnum;
import guda.house.biz.form.bill.ConfirmPayForm;
import guda.house.biz.form.bill.UnpaySearchForm;
import guda.house.biz.query.DepositRuleQuery;
import guda.house.biz.query.ExtRuleQuery;
import guda.house.biz.query.SysVarQuery;
import guda.house.biz.query.UnpayQuery;
import guda.house.biz.security.AppUtil;
import guda.house.biz.util.ErrorCode;
import guda.house.biz.util.PriceUtil;
import guda.house.dal.dao.DepositRuleDAO;
import guda.house.dal.dao.ExtRuleDAO;
import guda.house.dal.dataobject.DepositRuleDO;
import guda.house.dal.dataobject.ExtRuleDO;
import guda.house.dal.dataobject.SysVarDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by well on 15/11/17.
 */
@Controller
@RequestMapping(value = "bill/unpay")
public class UnpayAction {

    @Autowired
    private BillPayBiz billPayBiz;


    @RequestMapping(value = "list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap, UnpaySearchForm unpaySearchForm, BindingResult result) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = 20;
        int search = RequestUtil.getInt(request,"search");
        if(search == 0){
            BizResult bizResult = billPayBiz.initSearchForm(unpaySearchForm);
            modelMap.putAll(bizResult.data);
            return "bill/unpay/list.vm";
        }
//        if(org.springframework.util.StringUtils.isEmpty(unpaySearchForm.getFeeType())  && unpaySearchForm.getSuiteId() == null){
//            result.rejectValue("feeType","searchExceed","请缩小查询范围，查询某个业主的所有费用，或者查询某个小区的某项费用");
//            BizResult bizResult = billPayBiz.initSearchForm(unpaySearchForm);
//            modelMap.putAll(bizResult.data);
//            return "bill/unpay/list.vm";
//        }
        UnpayQuery unpayQuery = new UnpayQuery();
        unpayQuery.setPageSize(pageSize);
        unpayQuery.setPageNo(pageId);
        unpayQuery.setSuiteId(unpaySearchForm.getSuiteId());
        unpayQuery.setFeeType(unpaySearchForm.getFeeType());
        BizResult bizResult = billPayBiz.listForUnPay(unpayQuery);
        BizResult bizResult1 = billPayBiz.initSearchForm(unpaySearchForm);
        modelMap.putAll(bizResult1.data);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "bill/unpay/list.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }
    }


    @RequestMapping(value = "stat.htm", method = RequestMethod.GET)
    public String stat(HttpServletRequest request, ModelMap modelMap, UnpaySearchForm unpaySearchForm, BindingResult result) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = 20;
        int search = RequestUtil.getInt(request, "search");
        if(unpaySearchForm.getVillageId() == null) {
            List<Long> villageIdList = AppUtil.getVillageIdList();
            if (villageIdList.size() > 0) {
                unpaySearchForm.setVillageId(villageIdList.get(0));
            }
        }

        if(search == 0){

            BizResult bizResult = billPayBiz.initSearchForm(unpaySearchForm);
            modelMap.putAll(bizResult.data);
            return "bill/unpay/stat.vm";
        }
//        if(org.springframework.util.StringUtils.isEmpty(unpaySearchForm.getFeeType())  && unpaySearchForm.getSuiteId() == null){
//            result.rejectValue("feeType","searchExceed","请缩小查询范围，查询某个业主的所有费用，或者查询某个小区的某项费用");
//            BizResult bizResult = billPayBiz.initSearchForm(unpaySearchForm);
//            modelMap.putAll(bizResult.data);
//            return "bill/unpay/list.vm";
//        }

        UnpayQuery unpayQuery = new UnpayQuery();
        unpayQuery.setPageSize(pageSize);
        unpayQuery.setPageNo(pageId);
        unpayQuery.setSuiteId(unpaySearchForm.getSuiteId());
        unpayQuery.setVillageId(unpaySearchForm.getVillageId());
        unpayQuery.setBuildingId(unpaySearchForm.getBuildingId());
        unpayQuery.setUnitId(unpaySearchForm.getUnitId());
        unpayQuery.setOwnerName(unpaySearchForm.getOwnerName());
        unpayQuery.setParkingCard(unpaySearchForm.getParkingCard());
        unpayQuery.setParkingName(unpaySearchForm.getParkingName());
        unpayQuery.setFeeType(unpaySearchForm.getFeeType());
        BizResult bizResult = billPayBiz.statUnpay(unpayQuery);
        BizResult bizResult1 = billPayBiz.initSearchForm(unpaySearchForm);
        modelMap.putAll(bizResult1.data);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "bill/unpay/stat.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }
    }


    @RequestMapping(value = "confirmPay.htm",method = RequestMethod.GET )
    public String confirmPay(HttpServletRequest request, ModelMap modelMap, ConfirmPayForm confirmPayForm, BindingResult result) {

        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        int search = RequestUtil.getInt(request, "search");
        if(confirmPayForm.getVillageId() == null) {
            List<Long> villageIdList = AppUtil.getVillageIdList();
            if (villageIdList.size() > 0) {
                confirmPayForm.setVillageId(villageIdList.get(0));
            }
        }
        if(search == 0){
            BizResult bizResult = billPayBiz.initSearchForm(confirmPayForm);
            modelMap.putAll(bizResult.data);
            return "bill/unpay/confirmPay.vm";
        }
        if(org.springframework.util.StringUtils.isEmpty(confirmPayForm.getFeeType())  && confirmPayForm.getSuiteId() == null){
            result.rejectValue("feeType","searchExceed","请缩小查询范围，查询某个业主的所有费用，或者查询某个小区的某项费用");
            BizResult bizResult = billPayBiz.initSearchForm(confirmPayForm);
            modelMap.putAll(bizResult.data);
            return "bill/unpay/confirmPay.vm";
        }
        UnpayQuery unpayQuery = new UnpayQuery();
        unpayQuery.setPageSize(pageSize);
        unpayQuery.setPageNo(pageId);
        unpayQuery.setSuiteId(confirmPayForm.getSuiteId());
        unpayQuery.setFeeType(confirmPayForm.getFeeType());
        if(StringUtils.hasText(confirmPayForm.getStartDate())){
            String start = confirmPayForm.getStartDate()+"-01 00:00:00";
            SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date parse = simpleDateFormat.parse(start);
                unpayQuery.setStartDate(parse);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if(StringUtils.hasText(confirmPayForm.getEndDate())){
            String end = confirmPayForm.getEndDate()+"-01 00:00:00";
            SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date parse = simpleDateFormat.parse(end);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(parse);
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                calendar.set(Calendar.HOUR_OF_DAY,23);
                calendar.set(Calendar.MINUTE,59);
                calendar.set(Calendar.SECOND, 59);
                unpayQuery.setEndDate(calendar.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        BizResult bizResult = billPayBiz.listForUnPay(unpayQuery);
        BizResult bizResult1 = billPayBiz.initSearchForm(confirmPayForm);
        modelMap.putAll(bizResult.data);
        modelMap.putAll(bizResult1.data);
        return "bill/unpay/confirmPay.vm";
    }

    @RequestMapping(value = "confirmPay.htm",method = RequestMethod.POST )
    public String pay(HttpServletRequest request, ModelMap modelMap, @Valid ConfirmPayForm confirmPayForm, BindingResult result) {

        if(result.hasErrors()){
            BizResult bizResult = billPayBiz.initSearchForm(confirmPayForm);
            modelMap.putAll(bizResult.data);
            return "bill/unpay/confirmPay.vm";
        }
        Map<String, String[]> parameterMap = request.getParameterMap();

        BizResult bizResult = billPayBiz.pay(confirmPayForm, parameterMap);
        modelMap.putAll(bizResult.data);
        Long billPayId = (Long)bizResult.data.get("payId");
        return "redirect:/bill/pay/detail.htm?billPayId="+billPayId;
    }


    @RequestMapping(value = "doCancelBill.htm",method = RequestMethod.POST )
    public void cancelBill(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {

        String billId = request.getParameter("id");
        JsonResult result = JsonResult.getInstance();
        if(StringUtils.isEmpty(billId)){
            result.setErrMsg(ErrorCode.getMessage(CommonResultCode.PARAM_MISS));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        BizResult bizResult = billPayBiz.cancelBill(billId);
        if(bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
        }else{
            result.setErrMsg(ErrorCode.getMessage(CommonResultCode.UNKOWN_ERROR));
            ResponseHelper.ajaxReturn(result.getMap(), response);
        }
        return;
    }



}
